package com.cll.pmis.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cll.pmis.common.Constants;
import com.cll.pmis.common.Result;
import com.cll.pmis.common.annotation.HasPerms;
import com.cll.pmis.entity.Drug;
import com.cll.pmis.entity.Output;
import com.cll.pmis.service.IOutputService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 药品出库
 * </p>
 *
 * @author chenliangliang
 * @since 2018-06-18
 */
@RestController
@RequestMapping("/output")
public class OutputController {


    private IOutputService outputService;
    private JmsTemplate jmsTemplate;

    protected OutputController(IOutputService outputService, JmsTemplate jmsTemplate) {
        this.outputService = outputService;
        this.jmsTemplate = jmsTemplate;
    }


    @GetMapping("/list")
    public Result list(@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                       @RequestParam(value = "pageSize", required = false) Integer pageSize,
                       @RequestParam(value = "query", required = false) String query,
                       @RequestParam(value = "type", required = false) String type) {

        if (pageIndex == null || pageIndex < 1) {
            pageIndex = 1;
        }

        if (pageSize == null || pageSize < 1) {
            pageSize = 4;
        }

        Integer outputId = null;
        if (StringUtils.isNotEmpty(query)) {
            try {
                outputId = Integer.valueOf(query);
            } catch (Exception e) {
                //ignore
            }
        }

        Page<Output> page = outputService.selectPage(new Page<>(pageIndex, pageSize,"id",false),
                new EntityWrapper<Output>().like(outputId != null, "id", String.valueOf(outputId))
                        .eq(StringUtils.isNotEmpty(type), "type", type));
        return Result.ok(page);
    }


    @GetMapping("/delete")
    public Result delete(@RequestParam(value = "outputId",required = false) Integer outputId){
        if (outputId==null){
            return Result.paramError();
        }

        Output output = outputService.selectById(outputId);
        if (output==null){
            return Result.fail("出库单不存在");
        }

        if (StringUtils.equals(output.getType(),Constants.OUTPUT_TYPE_DOCTOR)){
            return Result.fail("无法删除该出库单，请联系开单医生");
        }

        if (output.getStatus()>Constants.OUTPUT_STATUS_UNCHECK){
            return Result.fail("该出库单已审核通过，不能删除");
        }

        return outputService.deleteById(outputId)?Result.ok(""):Result.deleteError();
    }

    @HasPerms("output:check")
    @GetMapping("/check")
    public Result checkOutput(@RequestParam("outputId") Integer outputId) {

        Output output = outputService.selectOne(new EntityWrapper<Output>().eq("id", outputId));

        if (output == null) {
            return Result.fail("该出库单不存在，请检查出库单号是否合法");
        }


        if (output.getStatus() < Constants.OUTPUT_STATUS_UNCHECK) {
            return Result.fail("该出库单未生效，等待支付中");
        } else if (output.getStatus() > Constants.OUTPUT_STATUS_UNCHECK) {
            return Result.fail("该订单已审核");
        }

        Output out = new Output();
        out.setId(outputId);
        out.setStatus(Constants.OUTPUT_STATUS_CHECK);
        out.setUpdateTime(new Date());

        if (outputService.updateById(out)) {
            jmsTemplate.convertAndSend(Constants.OUTPUT_DRUG_QUEUE, outputId);
            return Result.ok("");
        }
        return Result.fail("审核失败");
    }

    @GetMapping("/detail")
    public Result outputDetail(@RequestParam("outputId") Integer outputId) {
        if (outputId == null) {
            return Result.paramError();
        }

        Output output = outputService.selectOne(new EntityWrapper<Output>().eq("id", outputId));
        if (output == null) {
            return Result.fail("该出库单不存在");
        }

        List<Drug> drugs = outputService.getMapper().queryOutputDrug(outputId);

        output.setList(drugs);

        return Result.ok(output);
    }

}

